﻿using System;
using System.Collections.Generic;
using System.Text;
using jQueryCSharp.Extensions;

namespace jQueryCSharp.ui
{
    /// <summary>
    /// Configuração do ui.Dialog
    /// </summary>
    public class Dialog : jQueryCSharpBase
    {
        #region Constantes
        private const string OPENONCLICK = "";
        private const string BUTTONS = "";
        private const string CLOSETEXT = "Fechar";
        private const string POSITION = "center";
        private const string HIDE = null;
        private const string SHOW = null;
        private const string TITLE = "";

        private const int HEIGHT = 0;
        private const int MAXHEIGHT = 150;
        private const int MAXWIDTH = 150;
        private const int WIDTH = 1000;
        private const int ZINDEX = 1000;


        private const bool CLOSEONESCAPE = true;
        private const bool DISABLED = false;
        private const bool DRAGGABLE = true;
        private const bool AUTOOPEN = true;
        private const bool RESIZABLE = true;
        private const bool MODAL = false;
        #endregion

        private readonly List<string> _position;
        private readonly Dictionary<string, string> _buttons;

        /// <summary>
        /// 
        /// </summary>
        public Dialog( )
            : this( "" )
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="identifier"></param>
        public Dialog( string identifier )
        {
            _position = new List<string>( );
            _buttons = new Dictionary<string, string>( );

            Disabled = DISABLED;
            AutoOpen = AUTOOPEN;
            OpenOnClick = OPENONCLICK;
            CloseOnEscape = CLOSEONESCAPE;
            CloseText = CLOSETEXT;
            Draggable = DRAGGABLE;
            Height = HEIGHT;
            Hide = HIDE;
            MaxHeight = MAXHEIGHT;
            MaxWidth = MAXWIDTH;
            Modal = MODAL;
            Position.Add( POSITION );
            Resizable = RESIZABLE;
            Show = SHOW;
            Title = TITLE;
            Width = WIDTH;
            ZIndex = ZINDEX;

            if ( !String.IsNullOrEmpty( identifier ) )
            {
                if ( identifier.Contains( "#" ) )
                {
                    ID = identifier.Replace( "#", "" );
                }
                else
                {
                    Class = identifier.Replace( ".", "" );
                }
            }
        }

        #region Propriedades
        public bool Disabled { get; set; }
        public bool AutoOpen { get; set; }
        public string OpenOnClick { get; set; }
        public Dictionary<string, string> Buttons
        {
            get
            {
                return _buttons;
            }
        }
        public bool CloseOnEscape { get; set; }
        public string CloseText { get; set; }
        public bool Draggable { get; set; }
        public int Height { get; set; }
        public string Hide { get; set; }
        public int MaxHeight { get; set; }
        public int MaxWidth { get; set; }
        public bool Modal { get; set; }
        public IList<string> Position
        {
            get
            {
                return _position;
            }
        }
        public bool Resizable { get; set; }
        public string Show { get; set; }
        public string Title { get; set; }
        public int Width { get; set; }
        public int ZIndex { get; set; }
        #endregion

        public override string ToString( )
        {
            var sb = new StringBuilder( );

            if ( Disabled != DISABLED )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "disabled", Disabled.ToString( ).ToLower( ) ) );
            }

            if ( AutoOpen != AUTOOPEN )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "autoOpen", AutoOpen.ToString( ).ToLower( ) ) );
            }

            //if ( OpenOnClick != OPENONCLICK )
            //{
            //    //a validação é feita no Builder
            //}

            if ( Buttons.Count > 0 )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                var sbTmp = new StringBuilder( );

                sbTmp.Append( "{" );
                foreach ( var item in Buttons )
                {
                    sbTmp.Append( "'" );
                    sbTmp.Append( item.Key );
                    sbTmp.Append( "'" );
                    sbTmp.Append( ":" );
                    sbTmp.Append( item.Value );
                }
                sbTmp.Append( "}" );

                sb.Append( modelo.Formatar( "buttons", sbTmp.ToString( ) ) );
            }

            if ( CloseOnEscape != CLOSEONESCAPE )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "closeOnEscape", CloseOnEscape.ToString( ).ToLower( ) ) );
            }

            if ( CloseText != CLOSETEXT )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "closeText", "'{0}'".Formatar( CloseText ) ) );
            }

            if ( Draggable != DRAGGABLE )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "draggable", Draggable.ToString( ).ToLower( ) ) );
            }

            if ( Height != HEIGHT )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "height", Height.ToString( ) ) );
            }

            if ( Hide != HIDE )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "hide", "'{0}'".Formatar( Hide ) ) );
            }

            if ( MaxHeight != MAXHEIGHT )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "maxHeight", MaxHeight.ToString( ) ) );
            }

            if ( MaxWidth != MAXWIDTH )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "maxWidth", MaxWidth.ToString( ) ) );
            }

            if ( Modal != MODAL )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "modal", Modal.ToString( ).ToLower( ) ) );
            }

            if ( Position.Count > 0 )
            {
                var seguir = true;
                if ( Position.Count == 1 )
                {
                    if ( Position[0] == POSITION )
                    {
                        seguir = false;
                    }
                }

                if ( seguir )
                {
                    var tmp = new StringBuilder( );

                    if ( sb.Length > 0 ) sb.Append( "," );
                    foreach ( var item in Position )
                    {
                        if ( tmp.Length > 0 )
                        {
                            tmp.Append( ", " );
                        }
                        tmp.Append( "'" );
                        tmp.Append( item );
                        tmp.Append( "'" );
                    }

                    sb.Append( modelo.Formatar( "position", tmp.ToString( ) ) );
                }
            }

            if ( Resizable != RESIZABLE )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "resizable", Resizable.ToString( ).ToLower( ) ) );
            }

            if ( Show != SHOW )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "show", "'{0}'".Formatar( Show ) ) );
            }

            if ( Title != TITLE )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "title", "'{0}'".Formatar( Title ) ) );
            }

            if ( Width != WIDTH )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "width", Width.ToString( ) ) );
            }

            if ( ZIndex != ZINDEX )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "zIndex", ZIndex.ToString( ) ) );
            }

            if ( sb.Length > 0 )
            {
                return String.Concat( "{", sb.ToString( ), "}" );
            }

            return sb.ToString( );
        }
    }
}
